package tksDB

import (
	"github.com/astaxie/goredis"
	"strconv"
)

import (
	. "tksStatic"
	"tksUint"
)

//从缓存中读取游戏服务端列表，返回给客户端.
func GetAllServer(redis *goredis.Client, info *[]*GameServerInfo) error {
	var m map[string]string = make(map[string]string)
	err := redis.Hgetall("GateServerInfo", m)
	if err != nil {
		return err
	}
	for _, v := range m {
		var server GameServerInfo
		json, _ := tksUint.BufToMsg([]byte(v))
		serverId, _ := json.Get("ServerId").Int()
		server.ServerId = uint32(serverId)
		addr, _ := json.Get("Addr").String()
		server.Addr = addr
		name, _ := json.Get("Name").String()
		server.Name = name
		serverStatus, _ := json.Get("ServerStatus").Int()
		server.ServerStatus = uint32(serverStatus)
		ts, _ := json.Get("Ts").Int64()
		server.Ts = ts
		//
		*info = append(*info, &server)
	}
	return nil
}

//游戏服务端更新自己信息到缓存中.
func UpdateServerInfo(redis *goredis.Client, gsInfo *GameServerInfo) {
	buf, _ := tksUint.MsgToBuf(gsInfo)
	redis.Hset("GameServerInfo", strconv.FormatUint(uint64(gsInfo.ServerId), 10), buf)
}
